package assist.base

import java.io.File
import java.io.IOException
import javax.imageio.ImageIO

object A {
    @JvmStatic
    fun main(args: Array<String>) {
        try {
            // 读取图片
//               val image = ImageIO.read(File("/Users/wangpeng/Downloads/hpnew.png"))
               val image = ImageIO.read(File("D:\\artale\\ou\\xxx.png"))
//            val image = ImageIO.read(File("/Users/wangpeng/Downloads/keytext.png"))
            // 获取图像的宽和高
            val width = image.width
            val height = image.height
            // 创建一个 Map 用于存储颜色和对h应的次数
            val colorCountMap: MutableMap<String, Int> = HashMap()
            // 遍历每个像素
            for (y in 0 until height) {
                for (x in 0 until width) {
                    // 获取像素的颜色
                    val color = image.getRGB(x, y)
                    // 将颜色转换成字符串
                    val colorString = String.format("0x%08X", color)
                    // 更新 Map 中的值
                    colorCountMap[colorString] = colorCountMap.getOrDefault(colorString, 0) + 1
                }
            }
            val countLimit = 6
            colorCountMap.toSortedMap().forEach { (s, i) ->
                if (i > countLimit) {
                    println("${s}.toInt().rgb,")
                }
            }
            println("=======全信息========")
            println("=======全信息========")
            colorCountMap.toSortedMap().forEach { (s, i) ->
                if (i > countLimit) {
                    println("${s}.toInt().rgb    Count: $i")
                }
            }
            println("=======深黑========")
            println("=======深黑========")
            colorCountMap
                .toSortedMap()
                .forEach { (s, i) ->
                    if (i > countLimit) {
                        val black = (s[4] == '0' || s[4] == '1' || s[4] == '2')
                                && (s[6] == '0' || s[6] == '1' || s[6] == '2')
                                && (s[8] == '0' || s[8] == '1' || s[8] == '2')
                        if (black) {
                            println("${s}.toInt().rgb,")
                        }
                    }

            }

            println("=======红========")
            colorCountMap
                .toSortedMap()
                .forEach { (s, i) ->
                    if (i > countLimit) {
                        val black = (s[4] == 'A' || s[4] == 'B' || s[4] == 'C' || s[4] == 'D')
                                && (s[6] == '4' || s[6] == '5' || s[6] == '6' || s[6] == '7')
                        if (black) {
                            println("${s}.toInt().rgb(4),")
                        }
                    }

                }

        } catch (e: IOException) {
            e.printStackTrace()
        }
    }
}